package com.ltkj.baseplatform.controller;

import com.ltkj.baseplatform.service.IWXUserService;
import com.ltkj.baseplatform.service.IsendMessage;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: chenzeen
 * @Date: 2023/08/31
 * @Description:
 */
@Api(tags = "微信端认证接口")
@RestController
@RequestMapping("/wxAuth")
public class PubAuthController {
    @Autowired
    IsendMessage sendMessage;


    @Autowired
    IWXUserService wxUserService;

    /**
     * 根据手机号发送短信
     * @param phone
     * @return
     */
    @GetMapping("/sendMessage")
    @ApiOperation("发送短信")
    @Log(title = "发送短信", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name  = "phone", value = "手机号", required = true)
    public AjaxResult sendMessage(String phone) {
        if(StringUtils.isEmpty(phone)) {
            return AjaxResult.error("手机号不能为空");
        }
        return sendMessage.sendmessage(phone);
    }

    /**
     * 验证手机号
     * @return
     */
    @ApiOperation("验证手机号")
    @PostMapping("/checkPhoneCode")
    @Log(title = "验证手机号", businessType = BusinessType.OTHER)
   /* @ApiImplicitParams({
        @ApiImplicitParam(name="phone", value = "手机号", required = true),
        @ApiImplicitParam(name="code", value = "验证码", required = true)
    })*/
    public AjaxResult checkPhoneCode(@RequestBody Map<String,String> data)
    {
        if(StringUtils.isEmpty(data.get("phone"))) {
            return AjaxResult.error("手机号不能为空！");
        }
        if(StringUtils.isEmpty(data.get("code"))) {
            return AjaxResult.error("验证码不能为空！");
        }
        return sendMessage.checkPhoneCode(data.get("phone"),data.get("code"));
    }

    /**
     * 验证身份证,验证通过回更新用户拓展表
     * @param data
     * @return
     */
    @ApiOperation("验证身份号")
    @PostMapping("/checkIdCard")
    @Log(title = "验证身份号", businessType = BusinessType.OTHER)
    /*@ApiImplicitParams({
            @ApiImplicitParam(name="idCard", value = "身份证", required = true),
            @ApiImplicitParam(name="name", value = "姓名", required = true)
    })*/
    public AjaxResult checkIdCard(@RequestBody Map<String,String> data) {
        if(StringUtils.isEmpty(data.get("idCard"))) {
            return AjaxResult.error("idCard不能为空");
        }
        if(StringUtils.isEmpty(data.get("name"))) {
            return AjaxResult.error("name不能为空");
        }
        return wxUserService.checkIdCard(data.get("idCard"), data.get("name"));

    }

    /**
     * 获取EidToken
     * @param appKey 应用KEY，用于区分不同小程序。
     *                  -生态岛（可以不传）：island
     *                  -体育场：motion
     * @return token
     */
    @GetMapping("/getEidToken")
    @ApiOperation("获取EidToken")
    @Log(title = "获取EidToken", businessType = BusinessType.OTHER)
    public AjaxResult getEidToken(@ApiParam(value = "应用KEY，用于区分不同小程序。生态岛（可以不传）：island 体育场：motion") String appKey) {
        return wxUserService.getEidToken(appKey);
    }

    /**
     * 获取E证通结果信息
     */
    @GetMapping("/getEidResult")
    @ApiOperation("获取E证通结果信息")
    @Log(title = "获取E证通结果信息", businessType = BusinessType.OTHER)
    public AjaxResult getEidResult(String eidToken) {
        if (StringUtils.isEmpty(eidToken)) {
            return AjaxResult.error("EidToken不可以为空");
        }
        return wxUserService.getEidResult(eidToken);
    }

    @ApiOperation("测试分布式事务")
    @PostMapping("/testSeata")
    @Log(title = "验证身份号", businessType = BusinessType.OTHER)
    public AjaxResult testSeata() {
        return sendMessage.testSeata();
    }
}
